home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / compress / gnucpio.zip / USERSPEC.C < prev    next >
C/C++ Source or Header  |  1994-09-26  |  6KB  |  278 lines

  1. /* userspec.c -- Parse a user and group string.
  2.    Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23.  
  24. #ifdef __GNUC__
  25. #define alloca __builtin_alloca
  26. #else
  27. #ifdef HAVE_ALLOCA_H
  28. #include <alloca.h>
  29. #else
  30. #ifdef _AIX
  31.  #pragma alloca
  32. #else
  33. char *alloca ();
  34. #endif
  35. #endif
  36. #endif
  37.  
  38. #include <stdio.h>
  39. #include <sys/types.h>
  40. #include <pwd.h>
  41. #include <grp.h>
  42.  
  43. #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
  44. #include <string.h>
  45. #ifndef index
  46. #define index strchr
  47. #endif
  48. #else
  49. #include <strings.h>
  50. #endif
  51.  
  52. #ifdef STDC_HEADERS
  53. #include <stdlib.h>
  54. #endif
  55.  
  56. #ifdef HAVE_UNISTD_H
  57. #include <unistd.h>
  58. #endif
  59.  
  60. #ifndef _POSIX_VERSION
  61. struct passwd *getpwnam ();
  62. struct group *getgrnam ();
  63. struct group *getgrgid ();
  64. #endif
  65.  
  66. #ifdef _POSIX_SOURCE
  67. #define endpwent()
  68. #define endgrent()
  69. #endif
  70.  
  71. /* Perform the equivalent of the statement `dest = strdup (src);',
  72.    but obtaining storage via alloca instead of from the heap.  */
  73.  
  74. #define V_STRDUP(dest, src)                        \
  75.   do                                    \
  76.     {                                    \
  77.       int _len = strlen ((src));                    \
  78.       (dest) = (char *) alloca (_len + 1);                \
  79.       strcpy (dest, src);                        \
  80.     }                                    \
  81.   while (0)
  82.  
  83. #define isdigit(c) ((c) >= '0' && (c) <= '9')
  84.  
  85. char *strdup ();
  86.  
  87. /* Return nonzero if STR represents an unsigned decimal integer,
  88.    otherwise return 0. */
  89.  
  90. static int
  91. isnumber (str)
  92.      const char *str;
  93. {
  94.   for (; *str; str++)
  95.     if (!isdigit (*str))
  96.       return 0;
  97.   return 1;
  98. }
  99.  
  100. /* Extract from NAME, which has the form "[user][:.][group]",
  101.    a USERNAME, UID U, GROUPNAME, and GID G.
  102.    Either user or group, or both, must be present.
  103.    If the group is omitted but the ":" or "." separator is given,
  104.    use the given user's login group.
  105.  
  106.    USERNAME and GROUPNAME will be in newly malloc'd memory.
  107.    Either one might be NULL instead, indicating that it was not
  108.    given and the corresponding numeric ID was left unchanged.
  109.  
  110.    Return NULL if successful, a static error message string if not.  */
  111.  
  112. const char *
  113. parse_user_spec (spec_arg, uid, gid, username_arg, groupname_arg)
  114.      const char *spec_arg;
  115.      uid_t *uid;
  116.      gid_t *gid;
  117.      char **username_arg, **groupname_arg;
  118. {
  119.   static const char *tired = "virtual memory exhausted";
  120.   const char *error_msg;
  121.   char *spec;            /* A copy we can write on.  */
  122.   struct passwd *pwd;
  123.   struct group *grp;
  124.   char *g, *u, *separator;
  125.   char *groupname;
  126.  
  127.   error_msg = NULL;
  128.   *username_arg = *groupname_arg = NULL;
  129.   groupname = NULL;
  130.  
  131.   V_STRDUP (spec, spec_arg);
  132.  
  133.   /* Find the separator if there is one.  */
  134.   separator = index (spec, ':');
  135.   if (separator == NULL)
  136.     separator = index (spec, '.');
  137.  
  138.   /* Replace separator with a NUL.  */
  139.   if (separator != NULL)
  140.     *separator = '\0';
  141.  
  142.   /* Set U and G to non-zero length strings corresponding to user and
  143.      group specifiers or to NULL.  */
  144.   u = (*spec == '\0' ? NULL : spec);
  145.  
  146.   g = (separator == NULL || *(separator + 1) == '\0'
  147.        ? NULL
  148.        : separator + 1);
  149.  
  150.   if (u == NULL && g == NULL)
  151.     return "can not omit both user and group";
  152.  
  153.   if (u != NULL)
  154.     {
  155.       pwd = getpwnam (u);
  156.       if (pwd == NULL)
  157.     {
  158.  
  159.       if (!isnumber (u))
  160.         error_msg = "invalid user";
  161.       else
  162.         {
  163.           int use_login_group;
  164.           use_login_group = (separator != NULL && g == NULL);
  165.           if (use_login_group)
  166.         error_msg = "cannot get the login group of a numeric UID";
  167.           else
  168.         *uid = atoi (u);
  169.         }
  170.     }
  171.       else
  172.     {
  173.       *uid = pwd->pw_uid;
  174.       if (g == NULL && separator != NULL)
  175.         {
  176.           /* A separator was given, but a group was not specified,
  177.              so get the login group.  */
  178.           *gid = pwd->pw_gid;
  179.           grp = getgrgid (pwd->pw_gid);
  180.           if (grp == NULL)
  181.         {
  182.           /* This is enough room to hold the unsigned decimal
  183.              representation of any 32-bit quantity and the trailing
  184.              zero byte.  */
  185.           char uint_buf[21];
  186.           sprintf (uint_buf, "%u", (unsigned) (pwd->pw_gid));
  187.           V_STRDUP (groupname, uint_buf);
  188.         }
  189.           else
  190.         {
  191.           V_STRDUP (groupname, grp->gr_name);
  192.         }
  193.           endgrent ();
  194.         }
  195.     }
  196.       endpwent ();
  197.     }
  198.  
  199.   if (g != NULL && error_msg == NULL)
  200.     {
  201.       /* Explicit group.  */
  202.       grp = getgrnam (g);
  203.       if (grp == NULL)
  204.     {
  205.       if (!isnumber (g))
  206.         error_msg = "invalid group";
  207.       else
  208.         *gid = atoi (g);
  209.     }
  210.       else
  211.     *gid = grp->gr_gid;
  212.       endgrent ();        /* Save a file descriptor.  */
  213.  
  214.       if (error_msg == NULL)
  215.     V_STRDUP (groupname, g);
  216.     }
  217.  
  218.   if (error_msg == NULL)
  219.     {
  220.       if (u != NULL)
  221.     {
  222.       *username_arg = strdup (u);
  223.       if (*username_arg == NULL)
  224.         error_msg = tired;
  225.     }
  226.  
  227.       if (groupname != NULL && error_msg == NULL)
  228.     {
  229.       *groupname_arg = strdup (groupname);
  230.       if (*groupname_arg == NULL)
  231.         {
  232.           if (*username_arg != NULL)
  233.         {
  234.           free (*username_arg);
  235.           *username_arg = NULL;
  236.         }
  237.           error_msg = tired;
  238.         }
  239.     }
  240.     }
  241.  
  242.   return error_msg;
  243. }
  244.  
  245. #ifdef TEST
  246.  
  247. #define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s))
  248.  
  249. int
  250. main (int argc, char **argv)
  251. {
  252.   int i;
  253.  
  254.   for (i = 1; i < argc; i++)
  255.     {
  256.       const char *e;
  257.       char *username, *groupname;
  258.       uid_t uid;
  259.       gid_t gid;
  260.       char *tmp;
  261.  
  262.       tmp = strdup (argv[i]);
  263.       e = parse_user_spec (tmp, &uid, &gid, &username, &groupname);
  264.       free (tmp);
  265.       printf ("%s: %u %u %s %s %s\n",
  266.           argv[i],
  267.           (unsigned int) uid,
  268.           (unsigned int) gid,
  269.           NULL_CHECK (username),
  270.           NULL_CHECK (groupname),
  271.           NULL_CHECK (e));
  272.     }
  273.  
  274.   exit (0);
  275. }
  276.  
  277. #endif
  278.